package cn.com.qingz.core.utils;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspWriter;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.com.qingz.core.constantes.BusinessConstantes;
import cn.com.qingz.core.constantes.SystemConstantes;
import cn.com.qingz.core.exception.DicmfException;
import cn.com.qingz.core.model.DefaultResp;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * @ClassName: PrintExpJsonUtils
 * @Description: TODO(产生异常时，返回特定的json数据给客户端)
 * @author wangmb@tydic.com
 * @date 2013-6-26 下午6:04:29
 * 
 */
public final class PrintExpJsonUtils implements SystemConstantes
{
	private static Log log = LogFactory.getLog(PrintExpJsonUtils.class);
	private static final String MSG = "内部异常";
	
	/**
	 * @Title: printExpJson
	 * @Description: TODO(输出json数据到客户端)
	 * @author wangmb@tydic.com
	 * @date 2013-6-26
	 * @param response
	 * @param e
	 *            void 返回类型
	 */
	public static void printExpJson(HttpServletResponse response,JspWriter out, Throwable e) {
		try {
			response.addHeader("Content-Type", "application/json;charset=UTF-8");
			DefaultResp<String> dr = new DefaultResp<String>(UE01,UE01_MSG);

			if (e.getCause() instanceof DicmfException|| e instanceof DicmfException) {
				DicmfException ee = (DicmfException) e;
				dr.setCode(ee.getErrorCode());
				dr.setData(ee.getCustomMsg());
			} else {
				if(log.isDebugEnabled())
					dr.setData(e.getMessage());
			}
			
			if(log.isInfoEnabled()&&e!=null)
				log.info(e.getMessage());
			
			final ObjectMapper objMapper = new ObjectMapper();

			out.write(objMapper.writeValueAsString(dr));
			out.flush();
			//out.close();
		} catch (IOException e1) {
			log.warn("该异常不做处理", e1);
		}
	}

	/**
	 * @Title: printExpJson
	 * @Description: TODO(输出json数据到客户端)
	 * @author wangmb@tydic.com
	 * @date 2013-6-26
	 * @param response
	 * @param e
	 *            void 返回类型
	 */
	public static void printExpJson(HttpServletResponse response, Exception e) {
		PrintWriter out;
		try {
			out = response.getWriter();
			response.addHeader("Content-Type", "application/json;charset=UTF-8");
			DefaultResp<String> dr = new DefaultResp<String>(UE01,UE01_MSG);
			
			if (e.getCause() instanceof DicmfException||e instanceof DicmfException) {
				//DicmfException ee = (DicmfException) e;
				Throwable ee = e.getCause();
				ee = ee==null?e:ee;
				try {
					dr.setCode(BeanUtils.getProperty(ee, "errorCode"));
					dr.setMsg(BeanUtils.getProperty(ee, "customMsg"));
					dr.setData(BeanUtils.getProperty(ee, "customMsg"));
				} catch (IllegalAccessException e1) {
					log.warn(e1.getMessage());
				} catch (InvocationTargetException e1) {
					log.warn(e1.getMessage());
				} catch (NoSuchMethodException e1) {
					log.warn(e1.getMessage());
				}
			}else if(e.getCause() instanceof java.lang.IllegalArgumentException||e instanceof java.lang.IllegalArgumentException ){
				dr.setCode(SystemConstantes.ERROR);
				dr.setMsg(e.getMessage()); 
			}else{
				if(log.isDebugEnabled())
					dr.setData(e.getMessage());
			}
			
			if(log.isInfoEnabled()&&e!=null)
				log.info(e.getMessage());
			
			final ObjectMapper objMapper = new ObjectMapper();

			out.write(objMapper.writeValueAsString(dr));
			out.flush();
			out.close();
		} catch (IOException e1) {
			log.warn("该异常不做处理", e1);
		}
	}

	/**
	 * @Title: printExpJson
	 * @Description: TODO(输出json数据到客户端)
	 * @author wangmb@tydic.com
	 * @date 2013-6-26
	 * @param response
	 * @param msg
	 *            错误信息描述 void 返回类型
	 */
	public static void printExpJson(HttpServletResponse response, String msg) {
		PrintWriter out;
		try {
			out = response.getWriter();
			response.addHeader("Content-Type", "application/json;charset=UTF-8");
			DefaultResp<String> dr = new DefaultResp<String>(UE01,UE01_MSG);

			dr.setData(msg);

			if(log.isInfoEnabled())
				log.info(msg);
			
			final ObjectMapper objMapper = new ObjectMapper();

			out.write(objMapper.writeValueAsString(dr));
			out.flush();
			out.close();
		} catch (IOException e1) {
			log.warn("该异常不做处理", e1);
		}
	}
	
	/**
	 * @Title: printExpJson
	 * @Description: TODO(输出json数据到客户端)
	 * @author wangmb@tydic.com
	 * @date 2013-7-6
	 * @param response
	 * @param out
	 * @param code
	 * @param msg 
	 * void  返回类型 
	 */
	public static void printExpJson(HttpServletResponse response,JspWriter out,String code, String msg) {
		try {
			response.addHeader("Content-Type", "application/json;charset=UTF-8");
			DefaultResp<String> dr = new DefaultResp<String>(UE01,UE01_MSG);

			dr.setData(msg);

			if(log.isInfoEnabled())
				log.info(msg);
			
			final ObjectMapper objMapper = new ObjectMapper();

			out.write(objMapper.writeValueAsString(dr));
			out.flush();
		} catch (IOException e1) {
			log.warn("该异常不做处理", e1);
		} 
	}
}
